import apiurl from '@/config/urlConfig'
import { axios } from '@/utils/request'

class Service {
  /**
   * 题目管理
   * @returns {Promise<*>}
   */
  async page (params) {
    const { code, data } = await axios({
      url: apiurl.url.exam.question.page,
      method: 'post',
      data: { ...params }
    }) || {}
    if (code === 200) {
      const { total, records } = data
      const { pageNumber, pageSize } = params
      return {
        total,
        records: records.map((m, inx) => ({ key: (pageNumber - 1) * pageSize + 1 + inx, ...m }))
      }
    }
    return { total: 0, records: [] }
  }

  async saveOrUpdate (params) {
    return await axios({
      url: apiurl.url.exam.question.save,
      method: 'post',
      data: { ...params }
    }) || {}
  }

  /**
   * 数据回显
   * @param params
   * @returns {Promise<*|{}>}
   */
  async getInfo (params) {
    return await axios({
      url: apiurl.url.exam.question.getInfo,
      method: 'post',
      data: { ...params }
    }) || {}
  }

  async edit (params) {
    return await axios({
      url: apiurl.url.exam.question.edit,
      method: 'post',
      data: { ...params }
    }) || {}
  }

  async del ({ id }) {
    return await axios({
      url: `${apiurl.url.exam.question.del}/${id}`,
      method: 'post',
      data: {}
    }) || {}
  }

  async delImageById ({ id }) {
    return await axios({
      url: `${apiurl.url.exam.question.delImageById}/${id}`,
      method: 'post',
      data: {}
    }) || {}
  }
}

const questionService = new Service()
export default questionService
